package com.hkc.huakang.hkcpaperless;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hkc.huakang.hkcpaperless.bin.ChannelList;
import com.hkc.huakang.hkcpaperless.bin.OrderDetail;
import com.hkc.huakang.hkcpaperless.bin.OutOrderDetail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class OrderDetailsAdapter extends RecyclerView.Adapter {

    Context   context;
    List<OrderDetail>  listOrderDetails=new  ArrayList<OrderDetail>();

    public OrderDetailsAdapter(Context   context){
        this.context=context;
    }
    public  void  setDate( List<OrderDetail> listOrderDetails){
        this.listOrderDetails.clear();
        this.listOrderDetails.addAll(listOrderDetails);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new Holder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        OrderDetail   od=  listOrderDetails.get(position);
       ((Holder) holder).drug_name.setText(od.getDrug_name());
       ((Holder) holder).drug_unit.setText(od.getDrug_spec());
       ((Holder) holder).sum.setText(od.getQuantity());
       ((Holder) holder).unit_.setText(od.getOrder_unit());
       ((Holder) holder).position.setText(od.getPosition());
        ((Holder) holder).position.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(lisntener!=null){
                    lisntener.Click(view,od);

                }
            }
        });
        if(od.isDispatch()){
            ((Holder) holder).position.setTextColor(Color.parseColor("#8E8584"));
            ((Holder) holder).unit_.setTextColor(Color.parseColor("#8E8584"));
            ((Holder) holder).sum.setTextColor(Color.parseColor("#8E8584"));
            ((Holder) holder).drug_unit.setTextColor(Color.parseColor("#8E8584"));
            ((Holder) holder).drug_name.setTextColor(Color.parseColor("#8E8584"));
            ((Holder) holder).position.setClickable(false);
        }else{
            ((Holder) holder).position.setTextColor(Color.parseColor("#FFFFFF"));
            ((Holder) holder).unit_.setTextColor(Color.parseColor("#000000"));
            ((Holder) holder).sum.setTextColor(Color.parseColor("#000000"));
            ((Holder) holder).drug_unit.setTextColor(Color.parseColor("#000000"));
            ((Holder) holder).drug_name.setTextColor(Color.parseColor("#000000"));
            ((Holder) holder).position.setClickable(true);

        }

    }

    @Override
    public int getItemCount() {
        //return 12;
        //return 20;
        return listOrderDetails==null?0:listOrderDetails.size();
    }

    OnPositionClickLisntener lisntener;

    public  interface   OnPositionClickLisntener{
       public  abstract   void  Click(View view,OrderDetail  od);
    }
    public void  setPositiListener(OnPositionClickLisntener od){
       this.lisntener=od;

    }
    private class Holder extends RecyclerView.ViewHolder {
        View inflate;
       public  TextView drug_unit,sum,drug_name,position,unit_;
        public Holder(View inflate) {
            super(inflate);
            this.inflate= inflate;
            position= (TextView)inflate.findViewById(R.id.position);
            drug_name= (TextView)inflate.findViewById(R.id.drug_name);
            drug_unit= (TextView)inflate.findViewById(R.id.drug_unit);
            sum= (TextView)inflate.findViewById(R.id.sum);
            unit_= (TextView)inflate.findViewById(R.id.unit_);

        }
    }
}
